package com.vhuomai.wechat.service.impl;

import com.quanyue.auto.util.GlobalContext;
import com.vhuomai.wechat.constant.TemplateCode;
import com.vhuomai.wechat.entity.BaseWxTemplate;
import com.vhuomai.wechat.mapper.WxTemplateMapper;
import com.vhuomai.wechat.service.WxTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author harris
 */
@Service
public class WxTemplateServiceImpl implements WxTemplateService {
    
    @Autowired
    private WxTemplateMapper templateMapper;
    
    @Override
    public List<BaseWxTemplate> findAll() {
        return templateMapper.findByMerchantNo(GlobalContext.merchantNo());
    }
    
    @Override
    public BaseWxTemplate findByCode(TemplateCode code) {
        return templateMapper.findByCode(GlobalContext.merchantNo(), code);
    }
    
    @Override
    public BaseWxTemplate add(BaseWxTemplate template) {
        template.setMerchantNo(GlobalContext.merchantNo());
        template.setCreateTime(LocalDateTime.now());
        template.setUpdateTime(template.getCreateTime());
        templateMapper.insert(template);
        return template;
    }
    
    @Override
    public void update(BaseWxTemplate template) {
        template.setUpdateTime(LocalDateTime.now());
        templateMapper.updateSelective(template);
    }
    
    @Override
    public void remove(int id) {
        templateMapper.deleteOne(id);
    }
}
